package com.blate.dogxiao.decoration;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 用纵向列表的分割线装饰器
 * 绘制逻辑:
 * 在每一个的条目的顶部预留偏移,准备进行分割线绘制.对第一个条目和最后一个条目进行特殊处理
 * 第一个条目的顶部可能不需要绘制,最后一个条目的底部可能需要绘制
 * {@link #getItemOffsets(Rect, View, RecyclerView, RecyclerView.State)}预留的位置和
 * {@link #onDrawOver(Canvas, RecyclerView, RecyclerView.State)}应该是一致的
 * <p>
 * tips:如果只是绘制简单的左右带有padding的分割线,其实有更简单的方法.
 * 1.设置条目的偏移
 * 2.绘制底层背景,调用{@link #onDraw(Canvas, RecyclerView, RecyclerView.State)}进行底层绘制,
 * 绘制一个左右是条目前景,中间是分割线颜色的矩形
 * (背景会透过偏移透出)
 * 这个方法从效率来说更高效,就是太搓了,一点都不优雅
 *
 * @author Az
 * on 2020/11/4
 */
public class VerticalListDivDecoration
        extends RecyclerView.ItemDecoration {

    private int mDivSize;
    private int mStartPadding;
    private int mEndPadding;
    private boolean mDrawFirstTop;
    private boolean mDrawLastBottom;

    private Rect mDivRect;
    private Paint mDivPaint;

    public VerticalListDivDecoration(@ColorInt int divColor, @Px int divSize,
                                     @Px int startPadding, @Px int endPadding,
                                     boolean drawFirstTop, boolean drawLastBottom) {
        this.mDivSize = divSize;
        this.mStartPadding = startPadding;
        this.mEndPadding = endPadding;
        this.mDrawFirstTop = drawFirstTop;
        this.mDrawLastBottom = drawLastBottom;

        mDivRect = new Rect();
        mDivPaint = new Paint();
        mDivPaint.setStyle(Paint.Style.FILL);
        mDivPaint.setColor(divColor);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i += 1) {
            View child = parent.getChildAt(i);
            if (parent.getChildAdapterPosition(child) == 0) {
                if (!mDrawFirstTop) {
                    //第一个如果不绘制,进行下一次循环
                    continue;
                }
            }
            //判断是否需要在底部绘制分割
            RecyclerView.Adapter<?> adapter = parent.getAdapter();
            if (adapter != null && parent.getChildAdapterPosition(child) == adapter.getItemCount() - 1) {
                if (mDrawLastBottom) {
                    mDivRect.left = child.getLeft() + mStartPadding;
                    mDivRect.top = child.getBottom();
                    mDivRect.right = child.getRight() - mEndPadding;
                    mDivRect.bottom = child.getBottom() + mDivSize;
                    c.drawRect(mDivRect, mDivPaint);
                }
            }
            //这里对每一个cell的顶部绘制分割
            mDivRect.left = child.getLeft() + mStartPadding;
            mDivRect.top = child.getTop() - mDivSize;
            mDivRect.right = child.getRight() - mEndPadding;
            mDivRect.bottom = child.getTop();
            c.drawRect(mDivRect, mDivPaint);
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        outRect.left = 0;
        outRect.right = 0;
        if (parent.getChildAdapterPosition(view) == 0) {
            outRect.top = mDrawFirstTop ? mDivSize : 0;
        } else {
            outRect.top = mDivSize;
        }
        RecyclerView.Adapter<?> adapter = parent.getAdapter();
        if (adapter != null && parent.getChildAdapterPosition(view) == adapter.getItemCount() - 1) {
            outRect.bottom = mDrawLastBottom ? mDivSize : 0;
        } else {
            outRect.bottom = 0;
        }
    }
}
